// const API_HOST = 'https://miniapp.yledanpei.com'  // 生产
 const API_HOST = 'http://192.168.0.111:8090'  // 开发 - 左
// const API_HOST = 'http://192.168.0.147:9999'  // 开发 - 右 

const API = {}

API.getHost = function () {
	return API_HOST;
}

API.request = function (url, method = "GET", data = {}, args = { token: true }) {

	return new Promise(function (resolve, reject) {
		let header = {}

		url = API_HOST + url;

		if (args.token) {  //token判断
			const token = '';   //api的token验证
			header.Authorization = 'wx' + wx.getStorageSync('token')
			if (token) {
				if (url.indexOf("?") > 0) {
					url = url + '&access_token=' + token;
				} else {
					url = url + '?access_token=' + token;
				}
			} else {
				// console.warn('[提示]','部分数据需要授权，检测出当前访问用户未授权登录小程序');
			}
		}
		// console.log(url)
		//console.log(data)
		console.log('全局token',header)
		wx.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: function (res) {
				//console.log(res);
				if (res.statusCode == 200) {
					resolve(res.data);
				} else if (res.data.code === "rest_post_invalid_page_number") {
					wx.showToast({
						title: '没有更多内容',
						mask: false,
						duration: 1000
					});
				} else {
					wx.showToast({
						title: "请求数据失败",
						duration: 1500
					});
					console.log(res.data.message);
					reject(res.data);
				}
			},
			fail: function (err) {
				console.log(err);
				reject(err);
			}
		})
	});

}

API.get = function (url, data = {}, args = { token: false }) {
	return API.request(url, "GET", data, args);
}

API.post = function (url, data, args = { token: true }) {
	return API.request(url, "POST", data, args);
}

/**
 * 从客户端会话选择文件
 */
API.chooseMessageFile = (data) => {
	const options = {
		type: data?.type || 'file',
		count: data?.count || 1,
		extension: data?.extension || ['xlsx', 'csv'],
	}
	return new Promise(function (resolve, reject) {
		wx.chooseMessageFile({
			...options,
			success: (res) => resolve(res),
			fail: () => reject()
		})
	})
}
/**
 * 自定义路径上传
 */
API.uploadFile = (url, data) => {
	return console.log('uploadFile----------------',url, data)
	return new Promise(function (resolve, reject) {
		wx.uploadFile({
			url: API_HOST + url,
			header: {
				Authorization: 'wx' + wx.getStorageSync('token')
			},
			filePath: data.file,
			name: 'file',
			formData: data,
			success: (res) => resolve(res),
			fail: () => reject()
		})
	})
}

/**
 * 自定义路径下载
 */
API.downloadFile = (url) => {

	wx.downloadFile({
		url: (url.indexOf('http') == -1 ? API_HOST : '') + url,
		header: {
			Authorization: 'wx' + wx.getStorageSync('token')
		},
		success: function (res) {
			var filePath = res.tempFilePath;
			console.log(filePath);
			wx.openDocument({
				filePath: filePath,
				success: function (res) {
					console.log('打开文档成功')
				},
				fail: function (res) {
					console.log(res);
				},
				complete: function (res) {
					console.log(res);
				}
			})
		},
		fail: function (res) {
			console.log('文件下载失败');
		},
	})
}


/**
 * 需要授权的接口调用
 * @param	{Function} fn
 * @return {Promise}
 */
API.guard = function (fn) {
	const self = this
	return function () {
		if (API.wxminiLogin()) {
			return fn.apply(self, arguments)
		} else {
			return API.getHomeData().then(res => {
				console.log('登录成功', res);
				return fn.apply(self, arguments)
			}, err => {
				console.log('登录失败', err);
				return err
			})
		}
	}
}

module.exports = API